package tongwentongshu.com.app.activity;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.RegeocodeResult;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.App;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.contract.SelectLocationContract;
import tongwentongshu.com.app.presenter.SelectLocationPresenter;

/**
 * Created by XiongBin on 2017/6/29.
 */

public class SelectLocationActivity extends BaseActivity implements SelectLocationContract.View {

    @BindView(R.id.list_loation)
    ListView mList;
    @BindView(R.id.tv_now_location)
    TextView now_location;

    private SelectLocationPresenter selectLocationPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_select_location;
    }

    @Override
    protected int getTitleId() {
        return R.string.select_location;
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void initData() {
        selectLocationPresenter = new SelectLocationPresenter(this);
        selectLocationPresenter.initLocation(this, getIntent());

    }

    @OnClick({R.id.tv_now_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_now_location:
                toFinish(0);
                break;
        }
    }

    @Override
    public void setLocationAdapter(ListAdapter adapter, PoiItem p) {
        now_location.setText(p.getTitle());
        mList.setAdapter(adapter);
        mList.setOnItemClickListener(onItemClickListener);
    }

    /**
     * 点击地址
     */
    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EventBus.getDefault().post(new OrderEvent(selectLocationPresenter.getPoi(position + 1)));
            toFinish(position + 1);
        }
    };

    private void toFinish(int index) {
        PoiItem p = selectLocationPresenter.getPoi(index);
        Bundle b = new Bundle();
        b.putString("poi_title", p.getTitle());
        Log.e("tag", p.getTitle());
        resultFinish(b, App.SelectLocationCode);
    }
}
